package com.sc.syllabus.xmls;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.sc.syllabus.R;
import com.srain.utils.LogUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

/**
 * Created by liushuanglong on 2017/10/9.
 */
public class XmlActivity extends Activity {

    private TextView tv;
    private TextView bt_dom, bt_sax, bt_pull;
    private XmlUtils xmlUtils;
    private List<Student> students;
    private Button btnsax, btndom, btnpullread, btnpullwrite;
    private ListView list;

    private ArrayList<Person> persons;
    private ArrayAdapter<Person> mAdapter;

    private View.OnClickListener mClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.bt_dom:
                    try {
//                        students = xmlUtils.dom2xml(getResources().getAssets().open("student.xml"));
//                        tv.setText(students.toString());
                        xmlUtils.modifyXml(XmlActivity.this, getResources().getAssets().open("student.xml"));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case R.id.bt_sax:
                    try {
                        students = xmlUtils.sax2xml(getResources().getAssets().open("student.xml"));
                        tv.setText(students.toString());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case R.id.bt_pull:
                    try {
                        students = xmlUtils.pull2xml(getResources().getAssets().open("student.xml"));
                        tv.setText(students.toString());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;


                case R.id.btnsax:
                    try {
                        persons = readxmlForSAX(getAssets().open("person1.xml"));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    mAdapter = new ArrayAdapter<Person>(XmlActivity.this, android.R.layout.simple_expandable_list_item_1, persons);
                    list.setAdapter(mAdapter);
                    break;
                case R.id.btndom:
                    DomHelper ds = new DomHelper();
                    persons = ds.queryXML(getApplicationContext());
                    mAdapter = new ArrayAdapter<Person>(XmlActivity.this, android.R.layout.simple_expandable_list_item_1, persons);
                    list.setAdapter(mAdapter);
                    break;
                case R.id.btnpullread:
                    //获取文件资源建立输入流对象
                    try {
                        InputStream is = getAssets().open("person3.xml");
                        persons = PullHelper.getPersons(is);
                        if (persons.equals(null)) {
                            Toast.makeText(getApplicationContext(), "呵呵", Toast.LENGTH_SHORT).show();
                        }
                        for (Person p1 : persons) {
                            Log.i("逗比", p1.toString());
                        }
                        mAdapter = new ArrayAdapter<Person>(XmlActivity.this, android.R.layout.simple_expandable_list_item_1, persons);
                        list.setAdapter(mAdapter);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case R.id.btnpullwrite:
                    Context context = getApplicationContext();
                    List<Person> persons = new ArrayList<Person>();
                    persons.add(new Person(21, "逗比1", 70));
                    persons.add(new Person(31, "逗比2", 50));
                    persons.add(new Person(11, "逗比3", 30));
                    File xmlFile = new File(context.getFilesDir(), "jay.xml");
                    if (xmlFile.isFile()) {
                        LogUtils.i("filePath", "filePath == " + xmlFile.getPath());
                    }
                    FileOutputStream fos;
                    try {
                        fos = new FileOutputStream(xmlFile);
                        PullHelper.save(persons, fos);
                        Toast.makeText(context, "文件写入完毕", Toast.LENGTH_SHORT).show();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    try {
                        readxmlForSAX(new FileInputStream(xmlFile));
                        mAdapter = new ArrayAdapter<Person>(XmlActivity.this, android.R.layout.simple_expandable_list_item_1, persons);
                        list.setAdapter(mAdapter);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xml);

        tv = (TextView) findViewById(R.id.text);
        bt_dom = (TextView) findViewById(R.id.bt_dom);
        bt_sax = (TextView) findViewById(R.id.bt_sax);
        bt_pull = (TextView) findViewById(R.id.bt_pull);

        bt_dom.setOnClickListener(mClickListener);
        bt_sax.setOnClickListener(mClickListener);
        bt_pull.setOnClickListener(mClickListener);

        xmlUtils = new XmlUtils();

        list = (ListView) findViewById(R.id.list);
        btnsax = (Button) findViewById(R.id.btnsax);
        btndom = (Button) findViewById(R.id.btndom);
        btnpullread = (Button) findViewById(R.id.btnpullread);
        btnpullwrite = (Button) findViewById(R.id.btnpullwrite);

        btnsax.setOnClickListener(mClickListener);
        btndom.setOnClickListener(mClickListener);
        btnpullread.setOnClickListener(mClickListener);
        btnpullwrite.setOnClickListener(mClickListener);
    }

    private ArrayList<Person> readxmlForSAX(InputStream is) throws Exception {
        //获取文件资源建立输入流对象
//        InputStream is = getAssets().open("person1.xml");
        //①创建XML解析处理器
        SaxHelper ss = new SaxHelper();
        //②得到SAX解析工厂
        SAXParserFactory factory = SAXParserFactory.newInstance();
        //③创建SAX解析器
        SAXParser parser = factory.newSAXParser();
        //④将xml解析处理器分配给解析器,对文档进行解析,将事件发送给处理器
        parser.parse(is, ss);
        is.close();
        return ss.getPersons();
    }

    public static void actionStart(Context context) {
        context.startActivity(new Intent(context, XmlActivity.class));
    }
}
